package com.jiankang.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.jiankang.entity.Product;
import com.jiankang.entity.User;
import com.jiankang.service.ProductService;
import com.jiankang.util.Data;
import com.jiankang.util.Page;
import com.jiankang.util.Pager;
@Controller
@RequestMapping("/mountinformation")
public class ProductController {
	@Resource
	private ProductService productService;
	@RequestMapping(value = "/selectproduct", method = RequestMethod.GET)
	public String selectproduct(Model model, HttpServletRequest req) {
		String pageNow = req.getParameter("pageNow");
		Page page = null;
		List<Product> product = null;
		int totalCount = (int) productService.getAllProductCount();
		if (pageNow != null) { 
			page = new Page(totalCount, Integer.parseInt(pageNow));
			product = productService.getProduct(page);
		} else {
			page = new Page(totalCount, 1);
			product = productService.getProduct(page);
		}
		model.addAttribute("page", page);
		model.addAttribute("product", product);
		return "imgtable";
	}
	@RequestMapping("/addproductjump")
	public String addproductjump() {
		return "admin/jump";
	}

	/**
	 * 一次上传多张图片
	 */
	@RequestMapping("/threeFile")
	public String threeFile(@RequestParam("file") CommonsMultipartFile files[],HttpServletRequest request, Model model,Product pro) {
		// 获得项目的路径
		ServletContext sc = request.getSession().getServletContext();
		// 上传位置
		String path = sc.getRealPath("/upload/product/") + "/"; // 设定文件保存的目录
		File f = new File(path);
		if (!f.exists())
			f.mkdirs();
		String picName=null;
		for (int i = 0; i < files.length; i++) {
			String newFileName = new Date().getTime()+".jpg"; 
			if (!files[i].isEmpty()) {
				try {
					FileOutputStream fos = new FileOutputStream(path
							+ newFileName);
					InputStream in = files[i].getInputStream(); 
					int b = 0;
					while ((b = in.read()) != -1) {
						fos.write(b);
					}
					fos.close();
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			pro.setClick(0);
			pro.setTime(new Date());
			pro.setImages("upload/product/"+newFileName);
			picName +="upload/product/"+newFileName+",";
		}
		pro.setLittleImages(picName.replaceAll("null", ""));
		productService.savehome(pro);	
		return this.selectproduct(model, request);

	}

	//删除
	@RequestMapping(value = "/productdelete/{id}")
	public String productdelete(@PathVariable("id") int id, Model model,HttpServletRequest request,User user, Pager pager) {
		productService.deletehome(id);
		return this.selectproduct(model, request);
	}
	//编辑
	@RequestMapping(value = "/productmodification/{id}")
	public String productmodification(@PathVariable("id") int id,Model model,Product pro) {
		pro=productService.formationdetails(id);
		model.addAttribute("pro", pro);
		return "admin/modification";
	}
	//修改
	@RequestMapping(value = "/utilitiesandaccountants")
	public String utilitiesandaccountants(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, Model model,Product pro) {
		if(file==null){
			productService.updatemodification(pro);
			return this.selectproduct(model, request);
		}else{
			String path = request.getSession().getServletContext().getRealPath("upload/product");  
			//重命名文件
			String fileName = new Date().getTime()+".jpg";  
			File targetFile = new File(path, fileName);  
			if(!targetFile.exists()){  
				targetFile.mkdirs();  
			} 
			//保存  
			try {   
				file.transferTo(targetFile);  
			} catch (Exception e) {  
				e.printStackTrace();  
			}  
			model.addAttribute("fileUrl", Data.URL.base+"/upload/product/"+fileName);  
			pro.setImages("upload/product/"+fileName);
			productService.updatemodification(pro);
			return this.selectproduct(model, request);
		}
		
		
	//	
		
		
		
		
		
		
		
		
	}
}
